#coding:utf8
'''
Created on 2017/2/23
4:07
@author: Moon
qq : 550302989
'''

import os,sys,re
project_path = os.path.abspath('../')
sys.path.append(project_path)

class Feature(object):

    def __init__(self):
        # 初始化 数据库连接 对象
        self.__toolsMysqlconnect = __import__("Tools.MySqlConnect",fromlist=True)
        self.__mysqlConnection = getattr(self.__toolsMysqlconnect,"MySqlConnection")()
        self.__conn,self.__cur = self.__mysqlConnection.getMySqlDBConnection()
        #print self.conn , self.cur

    # 功能查看 查看单个
    def findFeatureOne(self,sql):
        try:
            self.__cur.execute(sql)
            result = self.__cur.fetchone()
            if result:
                return {
                "code" : 0,
                "message" : "查找成功",
                "result" : result
            }
            else:
                return {
                "code" : 1,
                "message" : "查找失败",
                "result" : result
            }
        except Exception ,e:
            return {
                "code" : 1,
                "message" : "查找失败",
                "result" : e
            }

    # 功能查看 查看多个
    def findFeatureAll(self,sql):
        try:
            self.__cur.execute(sql)
            result = self.__cur.fetchall()
            if result:
                return {
                "code" : 0,
                "message" : "查找成功",
                "result" : result
            }
            else:
                return {
                "code" : 1,
                "message" : "查找失败",
                "result" : result
            }
        except Exception ,e:
            return {
                "code" : 1,
                "message" : "查找失败",
                "result" : e
            }

    # 功能添加 、 功能修改 、 功能删除
    def dataCommitFeature(self,sql):
        try:
            self.__cur.execute(sql)
            self.__conn.commit()
            return {
                "code" : 0,
                "message" : "成功"
            }
        except Exception,e:
            self.__conn.rollback()
            return {
                "code" : 1,
                "message" : "失败",
                "result" : e
            }

    # 释放mysql连接
    def __del__(self):
        self.__mysqlConnection.MySqlDBClose(self.__conn,self.__cur)
        print "释放数据库连接"

